package com.css.zfzx.fxpg.modules.buildings.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.fxpg.common.utils.BeanUtil;
import com.css.zfzx.fxpg.common.utils.EasyPoiExcelUtil;
import com.css.zfzx.fxpg.modules.buildings.data.repository.FxBuildingDataUtil;
import com.css.zfzx.fxpg.modules.buildings.data.repository.FxBuildingsNativeRepository;
import com.css.zfzx.fxpg.modules.buildings.data.repository.FxBuildingsRepository;
import com.css.zfzx.fxpg.modules.buildings.data.repository.entity.FxBuildingsEntity;
import com.css.zfzx.fxpg.modules.buildings.data.service.FxBuildingsService;
import com.css.zfzx.fxpg.modules.buildings.data.viewobjects.FxBuildingsQueryParams;
import com.css.zfzx.fxpg.modules.buildings.data.viewobjects.FxBuildingsVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.io.WKTReader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author andy
 * @date 2020-11-11
 */
@Service
public class FxBuildingsServiceImpl implements FxBuildingsService {

    @Autowired
    private FxBuildingsRepository fxBuildingsRepository;
    @Autowired
    private FxBuildingsNativeRepository fxBuildingsNativeRepository;

    @Autowired
    private FxBuildingDataUtil fxBuildingDataUtil;

    private static GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
    private static WKTReader reader = new WKTReader(geometryFactory);

    @Override
    public JSONObject queryFxBuildingss(FxBuildingsQueryParams queryParams, int curPage, int pageSize) {
        Page<FxBuildingsEntity> fxBuildingsPage = this.fxBuildingsNativeRepository.queryFxBuildings(queryParams, curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(fxBuildingsPage);
        return jsonObject;
    }

    @Override
    public List<FxBuildingsEntity> getFxBuildingss(FxBuildingsQueryParams queryParams) {
        List<FxBuildingsEntity> fxBuildingsPage = this.fxBuildingsNativeRepository.listFxBuildings(queryParams);
        return fxBuildingsPage;
    }

    @Override
    public JSONObject searchFxBuildings(FxBuildingsQueryParams queryParams) {
        List<FxBuildingsEntity> fxBuildings = this.fxBuildingsNativeRepository.searchFxBuildings(queryParams);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("zoom", queryParams.getZoom());
        jsonObject.put("data", fxBuildings);
        return jsonObject;
    }

    @Override
    public FxBuildingsEntity getFxBuildings(String id) {
        FxBuildingsEntity fxBuildings = this.fxBuildingsRepository.findById(id).orElse(null);
        return fxBuildings;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FxBuildingsEntity saveFxBuildings(FxBuildingsEntity fxBuildings) {
        /*String uuid = UUIDGenerator.getUUID();
        fxBuildings.setId(uuid);
        fxBuildings.setCreateUser(PlatformSessionUtils.getUserId());
        fxBuildings.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.fxBuildingsRepository.save(fxBuildings);*/
        List<FxBuildingsEntity> entities = new ArrayList<>();
        entities.add(fxBuildings);
        fxBuildingDataUtil.sqlBatchSaveEntities(entities);
        return fxBuildings;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FxBuildingsEntity updateFxBuildings(FxBuildingsEntity fxBuildings) {
        fxBuildings.setUpdateUser(PlatformSessionUtils.getUserId());
        fxBuildings.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        String id = fxBuildings.getId();
        FxBuildingsEntity one = fxBuildingsRepository.findById(id).get();
        BeanUtil.copyPropertiesIgnoreNull(fxBuildings, one);
        this.fxBuildingsRepository.save(one);
        return fxBuildings;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteFxBuildingss(List<String> ids) {
        List<FxBuildingsEntity> fxBuildingsList = this.fxBuildingsRepository.findAllById(ids);
        if (!PlatformObjectUtils.isEmpty(fxBuildingsList) && fxBuildingsList.size() > 0) {
            for (FxBuildingsEntity fxBuildings : fxBuildingsList) {
                this.fxBuildingsRepository.delete(fxBuildings);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

    @Override
    public String exportExcel(FxBuildingsQueryParams queryParams, HttpServletResponse response) {
        try {
            List<FxBuildingsEntity> provinceEntities = fxBuildingsNativeRepository.listFxBuildings(queryParams);
            // 深拷贝
            ArrayList<FxBuildingsVO> vos = new ArrayList<>();
            for (FxBuildingsEntity buildingEntity : provinceEntities) {
                FxBuildingsVO vo = new FxBuildingsVO();
                BeanUtils.copyProperties(buildingEntity, vo);
                vos.add(vo);
            }
            // 导出
            EasyPoiExcelUtil.exportExcel(vos, "房屋数据统计", "房屋数据", FxBuildingsVO.class, "房屋数据.xlsx", response);
            return "导出成功";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

}
